/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.ActividadDAO;
import org.trandio.dao.ActividadInterface;
import org.trandio.dao.ActividadpypDAO;
import org.trandio.dao.ActividadpypInterface;
import org.trandio.dao.ContratoDAO;
import org.trandio.dao.ContratoInterface;
import org.trandio.dao.EntidadDAO;
import org.trandio.dao.EntidadInterface;
import org.trandio.dao.GrupoactDAO;
import org.trandio.dao.GrupoactInterface;
import org.trandio.dao.LiquidacionDAO;
import org.trandio.dao.LiquidacionInterface;
import org.trandio.dao.ManualDAO;
import org.trandio.dao.ManualInterface;
import org.trandio.dao.MedicoDAO;
import org.trandio.dao.MedicoInterface;
import org.trandio.dao.PacienteDAO;
import org.trandio.dao.PacienteInterface;
import org.trandio.dao.ProgramapypDAO;
import org.trandio.dao.ProgramapypInterface;
import org.trandio.hb.TbLiquidacion;

/**
 *
 * @author root
 */
public class ValidarFormularioServlet extends HttpServlet {

    private ContratoInterface contratoInterface;
    private PacienteInterface pacienteInterface;
    private ActividadInterface actividadInterface;
    private GrupoactInterface grupoactInterface;
    private EntidadInterface entidadInterface;
    private MedicoInterface medicoInterface;
    private ManualInterface manualInterface;
    private ProgramapypInterface programapypInterface;
    private ActividadpypInterface actividadpypInterface;
    private TbLiquidacion liquidacion;
    private LiquidacionInterface iface;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int op = Integer.parseInt(request.getParameter("op"));

        contratoInterface = new ContratoDAO();
        pacienteInterface = new PacienteDAO();
        actividadInterface = new ActividadDAO();
        grupoactInterface = new GrupoactDAO();
        entidadInterface = new EntidadDAO();
        medicoInterface = new MedicoDAO();
        manualInterface = new ManualDAO();
        programapypInterface = new ProgramapypDAO();
        actividadpypInterface = new ActividadpypDAO();

        try {

            switch (op) {

                case 1:
                    String numero = request.getParameter("num");

                    try {
                        if (contratoInterface.consultarContrato(numero, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 2:
                    String numeroIde = request.getParameter("num");

                    try {
                        if (pacienteInterface.consultarPaciente(numeroIde, "", "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;


                case 3:
                    String cod = request.getParameter("num");
                    int manual = Integer.parseInt(request.getParameter("manual"));

                    try {
                        if (actividadInterface.consultarActividadManual(cod, manual)) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;


                case 4:
                    String codGru = request.getParameter("num");

                    try {
                        if (grupoactInterface.consultarGrupoact(codGru, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 5:
                    String codEnt = request.getParameter("num");

                    try {
                        if (entidadInterface.consultarEntidad(codEnt, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;


                case 6:
                    String numeroIdeMed = request.getParameter("num");

                    try {
                        if (medicoInterface.consultarMedico(numeroIdeMed, "", "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 7:
                    String codMan = request.getParameter("num");

                    try {
                        if (manualInterface.consultarManual(codMan, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 8:
                    String codPyp = request.getParameter("num");

                    try {
                        if (programapypInterface.consultarProgramapyp(codPyp, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 9:
                    String codAct = request.getParameter("num");

                    try {
                        if (actividadInterface.consultarActividadCodDes(codAct, "").isEmpty()) {
                            out.println("{\"error\":\"0\"}");
                        } else {
                            out.println("{\"error\":\"1\"}");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(ValidarFormularioServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 10:

                    iface = new LiquidacionDAO();
                    liquidacion = new TbLiquidacion();


                    int tipo_usuario = (request.getParameter("idtipo_usuario").equals("")) ? 0 : Integer.parseInt(request.getParameter("idtipo_usuario"));
                    int nivel = (request.getParameter("idnivel").equals("")) ? 0 : Integer.parseInt(request.getParameter("idnivel"));
                    int servicio = (request.getParameter("idservicio").equals("")) ? 0 : Integer.parseInt(request.getParameter("idservicio"));
                    int grupo_edad = (request.getParameter("idgrupo_edad").equals("")) ? 0 : Integer.parseInt(request.getParameter("idgrupo_edad"));
                    int tipo_afiliado = (request.getParameter("idtipo_afiliado").equals("")) ? 0 : Integer.parseInt(request.getParameter("idtipo_afiliado"));
                    int tipo_poblacion = (request.getParameter("idtipo_poblacion").equals("")) ? 0 : Integer.parseInt(request.getParameter("idtipo_poblacion"));
                    int sexo = (request.getParameter("idsexo").equals("")) ? 0 : Integer.parseInt(request.getParameter("idsexo"));

                    liquidacion.setId(Integer.parseInt(request.getParameter("id")));
                    liquidacion.setIdtipoUsuario(tipo_usuario);
                    liquidacion.setIdNivel(nivel);
                    liquidacion.setIdServicio(servicio);
                    liquidacion.setIdgrupoEdad(grupo_edad);
                    liquidacion.setIdtipoAfiliacion(tipo_afiliado);
                    liquidacion.setIdtipoPoblacion(tipo_poblacion);
                    liquidacion.setIdSexo(sexo);
                    liquidacion.setRegla(request.getParameter("descripcion"));

                    boolean val = iface.validarRegla(liquidacion);
                    if (val) {
                        out.println("{\"error\":\"0\"}");
                    } else {
                        out.println("{\"error\":\"1\"}");
                    }
                    break;
            }



        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
